package com.zhong.beautifulimageapp.fragments.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zhong.beautifulimageapp.R;
import com.zhong.beautifulimageapp.model.CommonVariable;
import com.zhong.beautifulimageapp.model.TopicHeader;
import com.zhong.beautifulimageapp.utils.ChangeTheHtmlImg;
import com.zhong.beautifulimageapp.utils.DateTimeDistance;
import com.zhong.beautifulimageapp.utils.WebViewContentStyle;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TopicDetailHeaderView {

    @BindView(R.id.topic_detail_header_view_title)
    TextView mTopicDetailHeaderViewTitle;
    @BindView(R.id.topic_detail_header_view_goodImg)
    ImageView mTopicDetailHeaderViewGoodImg;
    @BindView(R.id.topic_detail_header_view_user_icon)
    ImageView mTopicDetailHeaderViewUserIcon;
    @BindView(R.id.topic_detail_header_view_collect)
    ImageView mTopicDetailHeaderViewCollect;
    @BindView(R.id.topic_detail_button_classify)
    Button mTopicDetailButtonClassify;
    @BindView(R.id.topic_detail_header_view_user_name)
    TextView mTopicDetailHeaderViewUserName;
    @BindView(R.id.topic_detail_header_view_topicCreateTime)
    TextView mTopicDetailHeaderViewTopicCreateTime;
    @BindView(R.id.topic_detail_header_view_topicViewCount)
    TextView mTopicDetailHeaderViewTopicViewCount;
    @BindView(R.id.topic_detail_header_view_header_below_content)
    WebView mTopicDetailHeaderViewHeaderBelowContent;
    @BindView(R.id.topic_detail_header_view_not_have_replay)
    TextView mTopicDetailHeaderViewNotHaveReplay;
    @BindView(R.id.topic_detail_header_view_replay_count)
    TextView mTopicDetailHeaderViewReplayCount;
    @BindView(R.id.topic_detail_header_view_not_have_replay_below_shadowGap)
    View mTopicDetailHeaderViewNotHaveReplayBelowShadowGap;

    private Activity mActivity;

    public TopicDetailHeaderView(Activity activity, ListView listView) {
        mActivity = activity;
        View view = LayoutInflater.from(activity).inflate(R.layout.activity_topic_detail_header_view, listView, false);
        listView.addHeaderView(view, null, false);
        ButterKnife.bind(this, view);
    }

    public void updateView(TopicHeader topicHeader) {
        mTopicDetailHeaderViewTitle.setText(topicHeader.getTitle());
        if (topicHeader.isGood()) {
            mTopicDetailHeaderViewGoodImg.setVisibility(View.VISIBLE);
        } else {
            mTopicDetailHeaderViewGoodImg.setVisibility(View.GONE);
        }
        Glide.with(mActivity)
                .load(topicHeader.getAuthor().getAvatar_url())
                .asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.app)
                .into(new BitmapImageViewTarget(mTopicDetailHeaderViewUserIcon) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(mActivity.getResources(), resource);
                        drawable.setCircular(true);
                        mTopicDetailHeaderViewUserIcon.setImageDrawable(drawable);
                    }
                });
        if (topicHeader.isTop()) {
            mTopicDetailButtonClassify.setBackgroundColor(0xFF1be617);
            mTopicDetailButtonClassify.setText("置顶");
        } else {
            mTopicDetailButtonClassify.setBackgroundColor(0xFFE0E0E0);
            if (topicHeader.getTab() != null){
                switch (topicHeader.getTab()) {
                    case "ask":
                        mTopicDetailButtonClassify.setText("问答");
                        break;
                    case "share":
                        mTopicDetailButtonClassify.setText("分享");
                        break;
                    case "job":
                        mTopicDetailButtonClassify.setText("招聘");
                        break;
                    default:
                        break;
                }
            }else {
                mTopicDetailButtonClassify.setText("普通");
            }
        }
        mTopicDetailHeaderViewUserName.setText(topicHeader.getAuthor().getLoginname());
        mTopicDetailHeaderViewTopicCreateTime.setText(DateTimeDistance.getTimeDistance(topicHeader.getCreate_at(), 3));
        mTopicDetailHeaderViewTopicViewCount.setText(topicHeader.getVisit_count() + "次浏览");
        if (topicHeader.getReply_count() > 0) {
            mTopicDetailHeaderViewNotHaveReplayBelowShadowGap.setVisibility(View.GONE);
            mTopicDetailHeaderViewNotHaveReplay.setVisibility(View.GONE);
            mTopicDetailHeaderViewReplayCount.setText(topicHeader.getReply_count() + "条回复");
        } else {
            mTopicDetailHeaderViewNotHaveReplayBelowShadowGap.setVisibility(View.VISIBLE);
            mTopicDetailHeaderViewNotHaveReplay.setVisibility(View.VISIBLE);
            mTopicDetailHeaderViewReplayCount.setVisibility(View.GONE);
        }
        mTopicDetailHeaderViewHeaderBelowContent.setWebChromeClient(new WebChromeClient());
        mTopicDetailHeaderViewHeaderBelowContent.setWebViewClient(new WebViewClient());
        WebSettings settings = mTopicDetailHeaderViewHeaderBelowContent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false);
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String data = WebViewContentStyle.HTML_0 + WebViewContentStyle.LIGHT_THEME_CSS
                + WebViewContentStyle.HTML_1 + ChangeTheHtmlImg.changeImgSize(topicHeader.getContent())
                + "\n"
                + WebViewContentStyle.HTML_2;

        mTopicDetailHeaderViewHeaderBelowContent.loadDataWithBaseURL(null, data, "text/html;charset=UTF-8", null, null);
    }
}
